package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.GoodsNoticeApi;
import com.kgc.sbt.api.OrderApi;
import com.kgc.sbt.api.WxPayApi;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.constant.SystemConstant;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import com.kgc.sbt.util.WxpayUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created on 2021/6/23.
 * <p>
 * Author: yx
 * <p>
 * Description:
 */
@RequestPermission
@RestController
@Slf4j
@Api(tags = "到货通知接口，版本号：1.0.0")
public class GoodsNoticeController {


    @Reference
    private GoodsNoticeApi goodsNoticeApi;

    @Reference
    private OrderApi orderApi;

    @Autowired
    private RedisUtils redisUtils;

    /**
     *到货通知预约
     */
    @ApiOperation(value = "到货通知预约接口",notes = "需要传入商品id")
    @ApiResponses({
            @ApiResponse(code = 4010,message = "商品id错误，没有该商品"),
            @ApiResponse(code = 4009,message = "库存充足，无需预约"),
            @ApiResponse(code = 4000,message = "预约到货通知失败"),
            @ApiResponse(code = 0000,message = "预约到货通知成功"),
    })
    @PostMapping("/goodsNotice")
    public RequestResult<String> goodsNotice(HttpServletRequest request,@RequestParam @ApiParam(name = "goodsId",value = "商品id",required = true) Integer goodsId){

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);

        log.info("------ userId:" + userId);

        //查库存
        if(goodsNoticeApi.queryGoodsNum(goodsId)==null){

            return ResultBuildUtil.fail("4010","商品id错误，没有该商品");

        }else if(goodsNoticeApi.queryGoodsNum(goodsId)>0){
            return ResultBuildUtil.fail("4009","库存充足，无需预约");
        }


        if(goodsNoticeApi.GoodsNoticeBooking(userId,goodsId)){
            return ResultBuildUtil.success("预约到货通知成功");
        }

        return ResultBuildUtil.fail("4000","预约到货通知失败");
    }





}
